home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / libdes / cfb_enc.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  5KB  |  161 lines

  1. /* lib/des/cfb_enc.c */
  2. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  * 
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. #include "des_locl.h"
  49.  
  50. /* The input and output are loaded in multiples of 8 bits.
  51.  * What this means is that if you hame numbits=12 and length=2
  52.  * the first 12 bits will be retrieved from the first byte and half
  53.  * the second.  The second 12 bits will come from the 3rd and half the 4th
  54.  * byte.
  55.  */
  56. void des_cfb_encrypt(in, out, numbits, length, schedule, ivec, encrypt)
  57. unsigned char *in;
  58. unsigned char *out;
  59. int numbits;
  60. long length;
  61. des_key_schedule schedule;
  62. des_cblock (*ivec);
  63. int encrypt;
  64.     {
  65.     register unsigned long d0,d1,v0,v1,n=(numbits+7)/8;
  66.     register unsigned long mask0,mask1;
  67.     register unsigned long l=length;
  68.     register int num=numbits;
  69.     unsigned long ti[2];
  70.     unsigned char *iv;
  71.  
  72.     if (num > 64) return;
  73.     if (num > 32)
  74.         {
  75.         mask0=0xffffffffL;
  76.         if (num == 64)
  77.             mask1=mask0;
  78.         else    mask1=(1L<<(num-32))-1;
  79.         }
  80.     else
  81.         {
  82.         if (num == 32)
  83.             mask0=0xffffffffL;
  84.         else    mask0=(1L<<num)-1;
  85.         mask1=0x00000000;
  86.         }
  87.  
  88.     iv=(unsigned char *)ivec;
  89.     c2l(iv,v0);
  90.     c2l(iv,v1);
  91.     if (encrypt)
  92.         {
  93.         while (l >= n)
  94.             {
  95.             l-=n;
  96.             ti[0]=v0;
  97.             ti[1]=v1;
  98.             des_encrypt((unsigned long *)ti,schedule,DES_ENCRYPT);
  99.             c2ln(in,d0,d1,n);
  100.             in+=n;
  101.             d0=(d0^ti[0])&mask0;
  102.             d1=(d1^ti[1])&mask1;
  103.             l2cn(d0,d1,out,n);
  104.             out+=n;
  105.             /* 30-08-94 - eay - changed because l>>32 and
  106.              * l<<32 are bad under gcc :-( */
  107.             if (num == 32)
  108.                 { v0=v1; v1=d0; }
  109.             else if (num == 64)
  110.                 { v0=d0; v1=d1; }
  111.             else if (num > 32) /* && num != 64 */
  112.                 {
  113.                 v0=((v1>>(num-32))|(d0<<(64-num)))&0xffffffffL;
  114.                 v1=((d0>>(num-32))|(d1<<(64-num)))&0xffffffffL;
  115.                 }
  116.             else /* num < 32 */
  117.                 {
  118.                 v0=((v0>>num)|(v1<<(32-num)))&0xffffffffL;
  119.                 v1=((v1>>num)|(d0<<(32-num)))&0xffffffffL;
  120.                 }
  121.             }
  122.         }
  123.     else
  124.         {
  125.         while (l >= n)
  126.             {
  127.             l-=n;
  128.             ti[0]=v0;
  129.             ti[1]=v1;
  130.             des_encrypt((unsigned long *)ti,schedule,DES_ENCRYPT);
  131.             c2ln(in,d0,d1,n);
  132.             in+=n;
  133.             /* 30-08-94 - eay - changed because l>>32 and
  134.              * l<<32 are bad under gcc :-( */
  135.             if (num == 32)
  136.                 { v0=v1; v1=d0; }
  137.             else if (num == 64)
  138.                 { v0=d0; v1=d1; }
  139.             else if (num > 32) /* && num != 64 */
  140.                 {
  141.                 v0=((v1>>(num-32))|(d0<<(64-num)))&0xffffffffL;
  142.                 v1=((d0>>(num-32))|(d1<<(64-num)))&0xffffffffL;
  143.                 }
  144.             else /* num < 32 */
  145.                 {
  146.                 v0=((v0>>num)|(v1<<(32-num)))&0xffffffffL;
  147.                 v1=((v1>>num)|(d0<<(32-num)))&0xffffffffL;
  148.                 }
  149.             d0=(d0^ti[0])&mask0;
  150.             d1=(d1^ti[1])&mask1;
  151.             l2cn(d0,d1,out,n);
  152.             out+=n;
  153.             }
  154.         }
  155.     iv=(unsigned char *)ivec;
  156.     l2c(v0,iv);
  157.     l2c(v1,iv);
  158.     v0=v1=d0=d1=ti[0]=ti[1]=0;
  159.     }
  160.  
  161.